/*
 *    Copyright (c) 2018-2025, fanxl All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: fanxl (xunlian_fan001@126.com)
 */
package com.taiji.dicom4cloud.smart.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.taiji.dicom4cloud.common.core.exception.ServiceException;
import com.taiji.dicom4cloud.common.core.utils.CommonUtils;
import com.taiji.dicom4cloud.smart.dao.CarMapper;
import com.taiji.dicom4cloud.smart.model.entity.Car;
import com.taiji.dicom4cloud.smart.model.entity.CarOwer;
import com.taiji.dicom4cloud.smart.model.entity.Org;
import com.taiji.dicom4cloud.smart.service.CarOwerService;
import com.taiji.dicom4cloud.smart.service.CarService;
import com.taiji.dicom4cloud.smart.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 
 *
 * @author fanxl
 * @date 2021-07-06 14:01:20
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    private CarOwerService carOwerService;

    @Autowired
    private OrgService orgService;

    @Override
    public Car getByRfidNo(String no) {
        return this.getOne(Wrappers.<Car>lambdaQuery().eq(Car::getRfidCardNo,no));
    }

    @Override
    public Page queryPage(Page page, Car car) {
        return this.page(page, Wrappers.<Car>lambdaQuery()
                .eq(StringUtil.isNotEmpty(car.getOrgId())&&!car.getOrgId().equals("-1"),Car::getOrgId,car.getOrgId())
                .eq(Car::getDelFlag,"0")
                .like(StringUtil.isNotEmpty(car.getOwnerName()),Car::getOwnerName,car.getOwnerName())
                .like(StringUtil.isNotEmpty(car.getBrand()),Car::getBrand,car.getBrand()));
    }

    @Override
    public Boolean saveCar(Car car) {
        CarOwer carOwer = carOwerService.getById(car.getOwnerId());
        if(ObjectUtil.isNotNull(carOwer)){
            car.setOwnerName(carOwer.getOwnerName());
        }
        Org org = orgService.getById(car.getOrgId());
        if(ObjectUtil.isNotNull(org)){
            car.setOrgName(org.getOrgName());
        }
        car.setCarId(CommonUtils.getUUID());

        Car carInfo = this.getOne(
                Wrappers.<Car>lambdaQuery()
                        .eq(Car::getOwnerId,car.getOwnerId())
        .eq(Car::getOrgId,car.getOrgId())
        .eq(Car::getPlateNumber,car.getPlateNumber()));

        if(ObjectUtil.isNotEmpty(carInfo)){
            throw new ServiceException("当前车主在当前小区已存在车牌为"+car.getPlateNumber()+"的车辆");
        }

        return this.save(car);
    }


    @Override
    public Boolean bindRfidCardNo(Car car) {
        Car car1 = this.getById(car.getCarId());
        if(ObjectUtil.isNotEmpty(car1)){
            car1.setRfidCardNo(car.getRfidCardNo());
        }
        return this.saveOrUpdate(car1);
    }

    @Override
    public Boolean removeCars(Integer id) {
        List<Car> carList = this.list(Wrappers.<Car>lambdaQuery().eq(Car::getOrgId,id));
        if(ObjectUtil.isNotNull(carList)){
            for(Car car:carList){
                car.setDelFlag("1");
            }
        }
        return this.saveOrUpdateBatch(carList);
    }
}
